package com.qire.common.utils;

import android.app.AlertDialog;
import android.content.pm.PackageManager;

import com.qire.common.R;
import com.tbruyelle.rxpermissions3.RxPermissions;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;

public class PermissionsChecker {

    private final FragmentActivity activity;
    private final HandleBehavior handleBehavior;
    private String[] permissions;
    private String permissionTips    = "奇热漫画需要获取(存储空间)和(设备信息)权限，以保证漫画正常缓存下载以及您的账号安全";
    private String permissionDenyTip = "奇热漫画需要获取(存储空间)和(设备信息)权限，以保证漫画正常缓存下载以及您的账号安全\n请在【设置-应用-奇热漫画-权限】中打开相关权限，以正常使用奇热漫画功能";

    public enum ObtainWay {
        /** 默认选择，之前状态，可能授权也可能未授权 */
        defaultAllows,
        /** 请求授权，已授权 */
        requestObtain,
        /** 去设置手动设置 */
        manuallySetting
    }

    public PermissionsChecker(FragmentActivity activity, HandleBehavior handleBehavior) {
        this.activity = activity;
        this.handleBehavior = handleBehavior;
    }

    public PermissionsChecker setPermissionTips(String permissionTips) {
        this.permissionTips = permissionTips;
        return this;
    }
    public PermissionsChecker setPermissionDenyTip(String permissionDenyTip) {
        this.permissionDenyTip = permissionDenyTip;
        return this;
    }

    // 验证权限：true为没授权,false为授权
    private boolean verifyPermission(String permission) {
        return ContextCompat.checkSelfPermission(activity.getApplicationContext(), permission) == PackageManager.PERMISSION_DENIED;
    }

    // 申请权限，如果权限允许则调用处理事务，如过不允许则现申请权限再调用事务
    public boolean applyPermissions(String... permissions) {
        this.permissions = permissions;
        for (String permission : permissions) {
            if (verifyPermission(permission)) {
                showPermissionTip();
                return true;
            }
        }
        handleBehavior.doHandle(ObtainWay.requestObtain);
        return false;
    }

    private void showPermissionTip(){
        AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.style.AppTheme_PermissionTipsDialog);
        builder.setTitle("权限申请")
                .setMessage(permissionTips)
                .setCancelable(false)
                .setPositiveButton("好的", (dialog, which) -> permissionGrant())
                .create().show();
    }

    private void permissionGrant() {
        new RxPermissions(activity).request(permissions).subscribe(successful -> {
            if (successful) {
                handleBehavior.doHandle(ObtainWay.requestObtain);
            } else {
                showPermissionDenyTip();
            }
        });
    }

    private void showPermissionDenyTip() {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.style.AppTheme_PermissionTipsDialog);
        builder.setTitle("权限申请")
                .setMessage(permissionDenyTip)
                .setCancelable(false)
                .setPositiveButton("去设置", (dialog, which) -> {
                    OSUtil.launchSettingPanel(activity);
                    activity.finish();
                    handleBehavior.doHandle(ObtainWay.manuallySetting);
                })
                .setNegativeButton("知道了", (dialog, view) -> handleBehavior.doHandle(ObtainWay.defaultAllows))
                .create()
                .show();
    }

    public interface HandleBehavior {
        void doHandle(ObtainWay obtainWay);
    }

}
